package com.example.admin.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class Main2Activity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final MChart myChart = (MChart) findViewById(R.id.myChart);
        Button btn1 = (Button) findViewById(R.id.btn1);
        Button btn2 = (Button) findViewById(R.id.btn2);
        Button btn3 = (Button) findViewById(R.id.btn3);
        ArrayList<Float> mlist = new ArrayList<>();
        mlist.add(30f);//40
        mlist.add(60f);//60
        mlist.add(90f);//80
        myChart.setMileageList(mlist);
        ArrayList<Float> tlist = new ArrayList<>();
        tlist.add(50f);
        tlist.add(100f);
        tlist.add(150f);
        myChart.setTimeList(tlist);
        ArrayList<String> dlist = new ArrayList<>();
        String[] dates = new String[]{"一","二","三","四","五","六","日"};
        dlist.addAll(Arrays.asList(dates));
        setChartChange(myChart,9,dlist);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChange(0,myChart);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChange(1,myChart);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChange(2,myChart);
            }
        });
    }
    private void setChange(int type,MChart myChart){
        switch (type){
            case 0:
                ArrayList<String> dlist1 = new ArrayList<>();
                String[] dates = new String[]{"一","二","三","四","五","六","日"};
                dlist1.addAll(Arrays.asList(dates));
                setChartChange(myChart,7,dlist1);
                Toast.makeText(Main2Activity.this, "7天", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                ArrayList<String> dlist2 = new ArrayList<>();
                String[] dates2 = new String[]{"1","7","14","21","28","30"};
                dlist2.addAll(Arrays.asList(dates2));
                setChartChange(myChart,30,dlist2);
                Toast.makeText(Main2Activity.this, "30天", Toast.LENGTH_SHORT).show();
                break;
            default:
                ArrayList<String> dlist3 = new ArrayList<>();
                String[] dates3 = new String[]{"1","3","7","11","15","19","23","27","31"};
                dlist3.addAll(Arrays.asList(dates3));
                setChartChange(myChart,31,dlist3);
                Toast.makeText(Main2Activity.this, "31天", Toast.LENGTH_SHORT).show();
        }
    }
    private void setChartChange(MChart myChart,int count,ArrayList<String> list){
        myChart.setDatesList(list,count);

        ArrayList<Entry> mdlist = new ArrayList<>();
        ArrayList<Entry> tdlist = new ArrayList<>();
        for(int i=0;i<count;i++){
            mdlist.add(new Entry(i, (float) (Math.random()*80)));
            tdlist.add(new Entry(i, (float) (Math.random()*160)));
        }
        myChart.setMileageDataList(mdlist);
        myChart.setTimeDataList(tdlist);

        myChart.invalidate();
    }

}
